/**
 * Created by BIYY on 2018-6-23.
 */
export default {
    getParam(key:any) {
        return this.getParamAll()[key] || '';
    },

    getParamAll() {
        let param:any = {};
        const queryStr = location.href.split('?');
        if (queryStr.length < 2) {
            return param;
        }
        let params:any = queryStr[1].split('&');
        params.forEach((item:any) => {
            const arr = item.split('=');
            const key = arr[0];
            const value = arr[1];
            param[key] = value;
        });
        return param;
    },
    objToUrl(param:any, key:any, encode:any) {
        if (param == null) return '';
        let paramStr = '';
        let t = typeof param;
        if (t == 'string' || t == 'number' || t == 'boolean') {
            paramStr += '&' + key + '=' + (encode == null || encode ? encodeURIComponent(param) : param);
        } else {
            for (let i in param) {
                let k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
                paramStr += this.objToUrl(param[i], k, encode);
            }
        }
        return paramStr;
    },
    urlEncode(url:any, param:any, key:any, encode:any) {
        return url.concat('?').concat(this.objToUrl(param, key, encode).slice(1));
    }
};
